package cc.linker.steplink.crm.commons.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * @author Mengwei Zhao
 * @version 1.0
 *
 */
public class CalendarUtils {
	public final static String FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
	public final static String DAY_FORMAT = "yyyy-MM-dd";
	private final static String[] DAY_OF_WEEK = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};

	public static void main(String[] args) {
		System.out.println(getToday());
	}

	/**
	 * 获取今天的日期
	 *
	 * @return
	 */
	public static Date getToday() {
		Calendar today = Calendar.getInstance();
		return today.getTime();

	}

	public static String getChineseWeekDay(Date date) {
		if (null == date) {
			date = new Date();
		}
		Calendar day = Calendar.getInstance();
		day.setTime(date);
		return DAY_OF_WEEK[day.get(Calendar.DAY_OF_WEEK) - 1];

	}

	/**
	 * 获取昨天的日期
	 *
	 * @return
	 */
	public static Date getYesterday() {
		Calendar today = Calendar.getInstance();
		today.add(Calendar.DAY_OF_YEAR, -1);
		return today.getTime();
	}

	/**
	 * 获取昨天的0时
	 *
	 * @return
	 */
	public static Date getYesterdayDefault() {
		Calendar today = Calendar.getInstance();
		today.add(Calendar.DAY_OF_YEAR, -1);
		setZero(today);
		return today.getTime();
	}

	public static Calendar getYesterdayDefault(boolean isCalendar) {
		Calendar today = Calendar.getInstance();
		today.add(Calendar.DAY_OF_YEAR, -1);
		setZero(today);
		return today;
	}

	/**
	 * 获取明天的日期
	 *
	 * @return
	 */
	public static Date getTomorrow() {
		Calendar today = Calendar.getInstance();
		today.add(Calendar.DAY_OF_YEAR, 1);
		return today.getTime();
	}

	/**
	 * 获取明天的0时
	 *
	 * @return
	 */
	public static Date getTomorrowDefault() {
		Calendar today = Calendar.getInstance();
		today.add(Calendar.DAY_OF_YEAR, 1);
		setZero(today);
		return today.getTime();
	}

	/**
	 * 把当天日期置为00:00:00
	 *
	 * @param calendar
	 */
	public static void setZero(Calendar calendar) {
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
	}

	/**
	 * 获取当天00:00:00
	 *
	 * @param calendar
	 */
	public static Calendar getZero(Calendar calendar) {
		setZero(calendar);
		return calendar;
	}

	/**
	 * 获取本周起止时间
	 *
	 * @return
	 */
	public static Calendar[] getWeek() {
		Calendar today = Calendar.getInstance();
		return getWeek(today);
	}

	/**
	 * 获取上周起止时间
	 *
	 * @return
	 */
	public static Calendar[] getLastWeek() {
		Calendar today = Calendar.getInstance();
		today.add(Calendar.DAY_OF_YEAR, -Calendar.DAY_OF_WEEK);
		return getWeek(today);
	}

	/**
	 * 获取当周起止时间
	 *
	 * @param calendar
	 * @return
	 */
	public static Calendar[] getWeek(Calendar calendar) {
		Calendar[] period = new Calendar[2];

		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		setZero(calendar);

		Calendar start = Calendar.getInstance();
		Calendar end = Calendar.getInstance();


		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		start.setTime(calendar.getTime());
		period[0] = start;

		calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		end.setTime(calendar.getTime());
		period[1] = end;
		return period;
	}

	/**
	 * 获取本月起止时间
	 *
	 * @return
	 */
	public static Calendar[] getMonth() {
		Calendar today = Calendar.getInstance();
		return getMonth(today);
	}

	/**
	 * 获取上月起止时间
	 *
	 * @return
	 */
	public static Calendar[] getLastMonth() {
		Calendar today = Calendar.getInstance();
		today.set(Calendar.MONTH, today.get(Calendar.MONTH) - 1);
		return getMonth(today);
	}

	/**
	 * 获取当月起止时间
	 *
	 * @param calendar
	 * @return
	 */
	public static Calendar[] getMonth(Calendar calendar) {
		Calendar[] period = new Calendar[2];

		setZero(calendar);

		Calendar start = Calendar.getInstance();
		Calendar end = Calendar.getInstance();


		calendar.set(Calendar.DAY_OF_MONTH, 1);
		start.setTime(calendar.getTime());
		period[0] = start;

		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
		end.setTime(calendar.getTime());
		period[1] = end;
		return period;
	}

	/**
	 * 获取本季度起止时间
	 *
	 * @return
	 */
	public static Calendar[] getSeason() {
		Calendar today = Calendar.getInstance();
		return getSeason(today);
	}

	/**
	 * 获取上季度起止时间
	 *
	 * @return
	 */
	public static Calendar[] getLastSeason() {
		Calendar today = Calendar.getInstance();
		today.set(Calendar.MONTH, today.get(Calendar.MONTH) - 6);
		return getSeason(today);
	}

	/**
	 * 获取当季度起止时间
	 *
	 * @param calendar
	 * @return
	 */
	public static Calendar[] getSeason(Calendar calendar) {
		Calendar[] period = new Calendar[2];

		setZero(calendar);

		Calendar start = Calendar.getInstance();
		Calendar end = Calendar.getInstance();


		calendar.set(Calendar.DAY_OF_MONTH, 1);
		switch (calendar.get(Calendar.MONTH) % 3) {
			case 0:
				break;
			case 1:
				calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
				break;
			case 2:
				calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 2);
				break;

			default:
				break;
		}
		start.setTime(calendar.getTime());
		period[0] = start;

		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 3);
		end.setTime(calendar.getTime());
		period[1] = end;
		return period;
	}

	/**
	 * 获取本年起止时间
	 *
	 * @return
	 */
	public static Calendar[] getYear() {
		Calendar today = Calendar.getInstance();
		return getYear(today);
	}

	/**
	 * 获取上年起止时间
	 *
	 * @return
	 */
	public static Calendar[] getLastYear() {
		Calendar today = Calendar.getInstance();
		today.add(Calendar.YEAR, -1);
		return getYear(today);
	}

	/**
	 * 获取当当年起止时间
	 *
	 * @param calendar
	 * @return
	 */
	public static Calendar[] getYear(Calendar calendar) {
		Calendar[] period = new Calendar[2];

		setZero(calendar);

		Calendar start = Calendar.getInstance();
		Calendar end = Calendar.getInstance();


		calendar.set(Calendar.DAY_OF_YEAR, 1);
		start.setTime(calendar.getTime());
		period[0] = start;

		calendar.add(Calendar.YEAR, 1);
		end.setTime(calendar.getTime());
		period[1] = end;
		return period;
	}

	/**
	 * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
	 *
	 * @param nowDate   当前时间
	 * @param startDate 开始时间
	 * @param endDate   结束时间
	 * @return
	 * @author szh
	 */
	public static boolean isEffectiveDate(String nowDate, String startDate, String endDate) {
		try {
			String format = "HH:mm:ss";
			Date startTime = new SimpleDateFormat(format).parse(startDate);
			Date endTime = new SimpleDateFormat(format).parse(endDate);
			Date nowTime = new SimpleDateFormat(format).parse(nowDate);

			if (nowTime.getTime() == startTime.getTime()
					|| nowTime.getTime() == endTime.getTime()) {
				return true;
			}
			long now = nowTime.getTime();
			long begin = startTime.getTime();
			long end = endTime.getTime();

			if (now > begin && now < end) {
				return true;
			} else {
				return false;
			}
		} catch (Exception ex) {
			return false;
		}
	}

	/**
	 * @param nowTime      当前时间
	 * @param startTimeStr 开始时间
	 * @param endTimeStr   结束时间
	 * @return
	 * @author sunran   判断当前时间在时间区间内
	 */
	public static boolean isEffectiveDates(Date nowTime, String startTimeStr, String endTimeStr) {
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			Date startTime = df.parse(startTimeStr);
			Date endTime = df.parse(endTimeStr);

			if (nowTime.getTime() == startTime.getTime()
					|| nowTime.getTime() == endTime.getTime()) {
				return true;
			}

			Calendar date = Calendar.getInstance();
			date.setTime(nowTime);

			Calendar begin = Calendar.getInstance();
			begin.setTime(startTime);

			Calendar end = Calendar.getInstance();
			end.setTime(endTime);

			if (date.after(begin) && date.before(end)) {
				return true;
			} else {
				return false;
			}
		} catch (Exception ex) {
			return false;
		}
	}

	/**
	 * 判断当前时间距离第二天凌晨的秒数
	 *
	 * @return 返回值单位为[s:秒]
	 */
	public static Long getSecondsNextEarlyMorning() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_YEAR, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
	}

	/**
	 * 计算两个日期之间相差的天数
	 *
	 * @param smdate 较小的时间
	 * @param bdate  较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate, Date bdate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			smdate = sdf.parse(sdf.format(smdate));
			bdate = sdf.parse(sdf.format(bdate));
			Calendar cal = Calendar.getInstance();
			cal.setTime(smdate);
			long time1 = cal.getTimeInMillis();
			cal.setTime(bdate);
			long time2 = cal.getTimeInMillis();
			long between_days = (time2 - time1) / (1000 * 3600 * 24);

			return Integer.parseInt(String.valueOf(between_days));
		} catch (Exception ex) {
			return 0;
		}
	}
}
